package com.mycjj.android.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.bean.YuyueMonthStateBean;
import com.mycjj.android.bean.YuyueMonthStateBean.YuyueData;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;
import com.mycjj.android.util.Utils;

public class Point_SelectYuyueTypeActivity extends BaseActivity implements android.view.View.OnClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_yuyue_type);
		mActManager.pushActivity(YUYUE_KEY, this);
		init();
	}
	private TextView mTv_store_name,mTv_change_store_name;
	private View mRl_baoyang_item,mRl_weixiu_item;
	private String carId;
	private void init(){
		initHeader();
		carId = Preferences.getSharedPreferences(mApp, "default_car_id", carId);
		if(TextUtils.isEmpty(carId)){
			Toast.makeText(Point_SelectYuyueTypeActivity.this, "carId为空，请重新登录!", Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(Point_SelectYuyueTypeActivity.this,LoginActivity.class);
			startActivity(intent);
			Point_SelectYuyueTypeActivity.this.finish();
			return;
		}
		Log.e("mark", "Point_SelectYuyueTypeActivity init() carId:"+carId);
		mTv_store_name = (TextView)findViewById(R.id.tv_store_name);
		mTv_change_store_name = (TextView)findViewById(R.id.tv_change_store_name);
		mTv_change_store_name.setOnClickListener(this);
	}
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("选择预约类型");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mActManager.popActivity(YUYUE_KEY,Point_SelectYuyueTypeActivity.this);
			}
		});
		
		mRl_baoyang_item = findViewById(R.id.rl_baoyang_item);
		mRl_weixiu_item = findViewById(R.id.rl_weixiu_item);
		mRl_baoyang_item.setOnClickListener(this);
		mRl_weixiu_item.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mTv_store_name.setText(Preferences.getSharedPreferences(mApp, "storeName", "浙江奥同汽车有限公司"));
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.tv_change_store_name:
			Intent intent = new Intent(this, SelectStoreActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_baoyang_item:
			getPlanState("2");
			break;
		case R.id.rl_weixiu_item:
			getPlanState("1");
			break;
		}
	}
	
	private void getPlanState(final String type){
		String  storeId = Preferences.getSharedPreferences(mApp,"storeId", "");
		Log.e("mark", "storeId:"+storeId);
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		String month = cal.get(Calendar.YEAR)+"-"+Utils.getMonth(cal.get(Calendar.MONTH) + 1);
		Log.d("mark", "storeId:"+storeId+";month:"+month);
		RequestParams params = new RequestParams();
		params.put("store_id", storeId);
		params.put("month", month);
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request = new ProjectHttpRequestImpl(this, true);
		request.requestStoreGetMonthPlanWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				Log.i("info", "----ethan==="+content);
				YuyueMonthStateBean bean = dataManager.getYuyueMonthStateBean();
				if(bean.getCode() == 200){
					Intent intent = new Intent(Point_SelectYuyueTypeActivity.this,SelectYuyueDateActivity.class);
					intent.putExtra("type", type);
					intent.putExtra("dataList", (ArrayList<YuyueData>)bean.getData());
					intent.putExtra("carId", carId);
					startActivity(intent);
				}else{
					Toast.makeText(Point_SelectYuyueTypeActivity.this, bean.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	
	
}
